home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / FS / DCACHE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-17  |  22.2 KB  |  927 lines

  1. /*
  2.  * fs/dcache.c
  3.  *
  4.  * Complete reimplementation
  5.  * (C) 1997 Thomas Schoebel-Theuer,
  6.  * with heavy changes by Linus Torvalds
  7.  */
  8.  
  9. /*
  10.  * Notes on the allocation strategy:
  11.  *
  12.  * The dcache is a master of the icache - whenever a dcache entry
  13.  * exists, the inode will always exist. "iput()" is done either when
  14.  * the dcache entry is deleted or garbage collected.
  15.  */
  16.  
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/fs.h>
  20. #include <linux/malloc.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23.  
  24. #include <asm/uaccess.h>
  25.  
  26. #define DCACHE_PARANOIA 1
  27. /* #define DCACHE_DEBUG 1 */
  28.  
  29. /* For managing the dcache */
  30. extern unsigned long num_physpages, page_cache_size;
  31. extern int inodes_stat[];
  32. #define nr_inodes (inodes_stat[0])
  33.  
  34. kmem_cache_t *dentry_cache; 
  35.  
  36. /*
  37.  * This is the single most critical data structure when it comes
  38.  * to the dcache: the hashtable for lookups. Somebody should try
  39.  * to make this good - I've just made it work.
  40.  *
  41.  * This hash-function tries to avoid losing too many bits of hash
  42.  * information, yet avoid using a prime hash-size or similar.
  43.  */
  44. #define D_HASHBITS     10
  45. #define D_HASHSIZE     (1UL << D_HASHBITS)
  46. #define D_HASHMASK     (D_HASHSIZE-1)
  47.  
  48. static struct list_head dentry_hashtable[D_HASHSIZE];
  49. static LIST_HEAD(dentry_unused);
  50.  
  51. struct {
  52.     int nr_dentry;
  53.     int nr_unused;
  54.     int age_limit;        /* age in seconds */
  55.     int want_pages;        /* pages requested by system */
  56.     int dummy[2];
  57. } dentry_stat = {0, 0, 45, 0,};
  58.  
  59. static inline void d_free(struct dentry *dentry)
  60. {
  61.     if (dentry->d_op && dentry->d_op->d_release)
  62.         dentry->d_op->d_release(dentry);
  63.     if (dname_external(dentry)) 
  64.         kfree(dentry->d_name.name);
  65.     kmem_cache_free(dentry_cache, dentry); 
  66. }
  67.  
  68. /*
  69.  * Release the dentry's inode, using the fileystem
  70.  * d_iput() operation if defined.
  71.  */
  72. static inline void dentry_iput(struct dentry * dentry)
  73. {
  74.     struct inode *inode = dentry->d_inode;
  75.     if (inode) {
  76.         dentry->d_inode = NULL;
  77.         list_del(&dentry->d_alias);
  78.         INIT_LIST_HEAD(&dentry->d_alias);
  79.         if (dentry->d_op && dentry->d_op->d_iput)
  80.             dentry->d_op->d_iput(dentry, inode);
  81.         else
  82.             iput(inode);
  83.     }
  84. }
  85.  
  86. /*
  87.  * dput()
  88.  *
  89.  * This is complicated by the fact that we do not want to put
  90.  * dentries that are no longer on any hash chain on the unused
  91.  * list: we'd much rather just get rid of them immediately.
  92.  *
  93.  * However, that implies that we have to traverse the dentry
  94.  * tree upwards to the parents which might _also_ now be
  95.  * scheduled for deletion (it may have been only waiting for
  96.  * its last child to go away).
  97.  *
  98.  * This tail recursion is done by hand as we don't want to depend
  99.  * on the compiler to always get this right (gcc generally doesn't).
  100.  * Real recursion would eat up our stack space.
  101.  */
  102. void dput(struct dentry *dentry)
  103. {
  104.     int count;
  105.  
  106.     if (!dentry)
  107.         return;
  108.  
  109. repeat:
  110.     count = dentry->d_count - 1;
  111.     if (count != 0)
  112.         goto out;
  113.  
  114.     /*
  115.      * Note that if d_op->d_delete blocks,
  116.      * the dentry could go back in use.
  117.      * Each fs will have to watch for this.
  118.      */
  119.     if (dentry->d_op && dentry->d_op->d_delete) {
  120.         dentry->d_op->d_delete(dentry);
  121.  
  122.         count = dentry->d_count - 1;
  123.         if (count != 0)
  124.             goto out;
  125.     }
  126.  
  127.     if (!list_empty(&dentry->d_lru)) {
  128.         dentry_stat.nr_unused--;
  129.         list_del(&dentry->d_lru);
  130.     }
  131.     if (list_empty(&dentry->d_hash)) {
  132.         struct dentry * parent;
  133.  
  134.         list_del(&dentry->d_child);
  135.         dentry_iput(dentry);
  136.         parent = dentry->d_parent;
  137.         d_free(dentry);
  138.         if (dentry == parent)
  139.             return;
  140.         dentry = parent;
  141.         goto repeat;
  142.     }
  143.     list_add(&dentry->d_lru, &dentry_unused);
  144.     dentry_stat.nr_unused++;
  145.     /*
  146.      * Update the timestamp
  147.      */
  148.     dentry->d_reftime = jiffies;
  149.  
  150. out:
  151.     if (count >= 0) {
  152.         dentry->d_count = count;
  153.         return;
  154.     }
  155.  
  156.     printk(KERN_CRIT "Negative d_count (%d) for %s/%s\n",
  157.         count,
  158.         dentry->d_parent->d_name.name,
  159.         dentry->d_name.name);
  160.     *(int *)0 = 0;    
  161. }
  162.  
  163. /*
  164.  * Try to invalidate the dentry if it turns out to be
  165.  * possible. If there are other dentries that can be
  166.  * reached through this one we can't delete it.
  167.  */
  168. int d_invalidate(struct dentry * dentry)
  169. {
  170.     /*
  171.      * Check whether to do a partial shrink_dcache
  172.      * to get rid of unused child entries.
  173.      */
  174.     if (!list_empty(&dentry->d_subdirs)) {
  175.         shrink_dcache_parent(dentry);
  176.     }
  177.  
  178.     /*
  179.      * Somebody else still using it?
  180.      *
  181.      * If it's a directory, we can't drop it
  182.      * for fear of somebody re-populating it
  183.      * with children (even though dropping it
  184.      * would make it unreachable from the root,
  185.      * we might still populate it if it was a
  186.      * working directory or similar).
  187.      */
  188.     if (dentry->d_count > 1) {
  189.         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
  190.             return -EBUSY;
  191.     }
  192.  
  193.     d_drop(dentry);
  194.     return 0;
  195. }
  196.  
  197. /*
  198.  * Select less valuable dentries to be pruned when we need
  199.  * inodes or memory. The selected dentries are moved to the
  200.  * old end of the list where prune_dcache() can find them.
  201.  * 
  202.  * Negative dentries are included in the selection so that
  203.  * they don't accumulate at the end of the list. The count
  204.  * returned is the total number of dentries selected, which
  205.  * may be much larger than the requested number of inodes.
  206.  */
  207. int select_dcache(int inode_count, int page_count)
  208. {
  209.     struct list_head *next, *tail = &dentry_unused;
  210.     int found = 0;
  211.     int depth = dentry_stat.nr_unused >> 1;
  212.     unsigned long max_value = 4;
  213.  
  214.     if (page_count)
  215.         max_value = -1;
  216.  
  217.     next = tail->prev;
  218.     while (next != &dentry_unused && depth--) {
  219.         struct list_head *tmp = next;
  220.         struct dentry *dentry = list_entry(tmp, struct dentry, d_lru);
  221.         struct inode *inode = dentry->d_inode;
  222.         unsigned long value = 0;    
  223.  
  224.         next = tmp->prev;
  225.         if (dentry->d_count) {
  226.             dentry_stat.nr_unused--;
  227.             list_del(tmp);
  228.             INIT_LIST_HEAD(tmp);
  229.             continue;
  230.         }
  231.  
  232.         /*
  233.          * Select dentries based on the page cache count ...
  234.          * should factor in number of uses as well. We take
  235.          * all negative dentries so that they don't accumulate.
  236.          * (We skip inodes that aren't immediately available.)
  237.          */
  238.         if (inode) {
  239.             value = inode->i_nrpages;    
  240.             if (value >= max_value)
  241.                 continue;
  242.             if (inode->i_state || inode->i_count > 1)
  243.                 continue;
  244.         }
  245.  
  246.         /*
  247.          * Move the selected dentries behind the tail.
  248.          */
  249.         if (tmp != tail->prev) {
  250.             list_del(tmp);
  251.             list_add(tmp, tail->prev);
  252.         }
  253.         tail = tmp;
  254.         found++;
  255.         if (inode && --inode_count <= 0)
  256.             break;
  257.         if (page_count && (page_count -= value) <= 0)
  258.             break;
  259.     }
  260.     return found;
  261. }
  262.  
  263. /*
  264.  * Throw away a dentry - free the inode, dput the parent.
  265.  * This requires that the LRU list has already been
  266.  * removed.
  267.  */
  268. static inline void prune_one_dentry(struct dentry * dentry)
  269. {
  270.     struct dentry * parent;
  271.  
  272.     list_del(&dentry->d_hash);
  273.     list_del(&dentry->d_child);
  274.     dentry_iput(dentry);
  275.     parent = dentry->d_parent;
  276.     d_free(dentry);
  277.     dput(parent);
  278. }
  279.  
  280. /*
  281.  * Shrink the dcache. This is done when we need
  282.  * more memory, or simply when we need to unmount
  283.  * something (at which point we need to unuse
  284.  * all dentries).
  285.  */
  286. void prune_dcache(int count)
  287. {
  288.     for (;;) {
  289.         struct dentry *dentry;
  290.         struct list_head *tmp = dentry_unused.prev;
  291.  
  292.         if (tmp == &dentry_unused)
  293.             break;
  294.         dentry_stat.nr_unused--;
  295.         list_del(tmp);
  296.         INIT_LIST_HEAD(tmp);
  297.         dentry = list_entry(tmp, struct dentry, d_lru);
  298.         if (!dentry->d_count) {
  299.             prune_one_dentry(dentry);
  300.             if (!--count)
  301.                 break;
  302.         }
  303.     }
  304. }
  305.  
  306. /*
  307.  * Shrink the dcache for the specified super block.
  308.  * This allows us to unmount a device without disturbing
  309.  * the dcache for the other devices.
  310.  *
  311.  * This implementation makes just two traversals of the
  312.  * unused list.  On the first pass we move the selected
  313.  * dentries to the most recent end, and on the second
  314.  * pass we free them.  The second pass must restart after
  315.  * each dput(), but since the target dentries are all at
  316.  * the end, it's really just a single traversal.
  317.  */
  318. void shrink_dcache_sb(struct super_block * sb)
  319. {
  320.     struct list_head *tmp, *next;
  321.     struct dentry *dentry;
  322.  
  323.     /*
  324.      * Pass one ... move the dentries for the specified
  325.      * superblock to the most recent end of the unused list.
  326.      */
  327.     next = dentry_unused.next;
  328.     while (next != &dentry_unused) {
  329.         tmp = next;
  330.         next = tmp->next;
  331.         dentry = list_entry(tmp, struct dentry, d_lru);
  332.         if (dentry->d_sb != sb)
  333.             continue;
  334.         list_del(tmp);
  335.         list_add(tmp, &dentry_unused);
  336.     }
  337.  
  338.     /*
  339.      * Pass two ... free the dentries for this superblock.
  340.      */
  341. repeat:
  342.     next = dentry_unused.next;
  343.     while (next != &dentry_unused) {
  344.         tmp = next;
  345.         next = tmp->next;
  346.         dentry = list_entry(tmp, struct dentry, d_lru);
  347.         if (dentry->d_sb != sb)
  348.             continue;
  349.         if (dentry->d_count)
  350.             continue;
  351.         dentry_stat.nr_unused--;
  352.         list_del(tmp);
  353.         INIT_LIST_HEAD(tmp);
  354.         prune_one_dentry(dentry);
  355.         goto repeat;
  356.     }
  357. }
  358.  
  359. /*
  360.  * Check whether a root dentry would be in use if all of its
  361.  * child dentries were freed. This allows a non-destructive
  362.  * test for unmounting a device.
  363.  */
  364. int is_root_busy(struct dentry *root)
  365. {
  366.     struct dentry *this_parent = root;
  367.     struct list_head *next;
  368.     int count = root->d_count;
  369.  
  370. repeat:
  371.     next = this_parent->d_subdirs.next;
  372. resume:
  373.     while (next != &this_parent->d_subdirs) {
  374.         struct list_head *tmp = next;
  375.         struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
  376.         next = tmp->next;
  377.         /* Decrement count for unused children */
  378.         count += (dentry->d_count - 1);
  379.         if (!list_empty(&dentry->d_subdirs)) {
  380.             this_parent = dentry;
  381.             goto repeat;
  382.         }
  383.         /* root is busy if any leaf is busy */
  384.         if (dentry->d_count)
  385.             return 1;
  386.     }
  387.     /*
  388.      * All done at this level ... ascend and resume the search.
  389.      */
  390.     if (this_parent != root) {
  391.         next = this_parent->d_child.next; 
  392.         this_parent = this_parent->d_parent;
  393.         goto resume;
  394.     }
  395.     return (count > 1); /* remaining users? */
  396. }
  397.  
  398. /*
  399.  * Search the dentry child list for the specified parent,
  400.  * and move any unused dentries to the end of the unused
  401.  * list for prune_dcache(). We descend to the next level
  402.  * whenever the d_subdirs list is non-empty and continue
  403.  * searching.
  404.  */
  405. static int select_parent(struct dentry * parent)
  406. {
  407.     struct dentry *this_parent = parent;
  408.     struct list_head *next;
  409.     int found = 0;
  410.  
  411. repeat:
  412.     next = this_parent->d_subdirs.next;
  413. resume:
  414.     while (next != &this_parent->d_subdirs) {
  415.         struct list_head *tmp = next;
  416.         struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
  417.         next = tmp->next;
  418.         if (!dentry->d_count) {
  419.             list_del(&dentry->d_lru);
  420.             list_add(&dentry->d_lru, dentry_unused.prev);
  421.             found++;
  422.         }
  423.         /*
  424.          * Descend a level if the d_subdirs list is non-empty.
  425.          */
  426.         if (!list_empty(&dentry->d_subdirs)) {
  427.             this_parent = dentry;
  428. #ifdef DCACHE_DEBUG
  429. printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
  430. dentry->d_parent->d_name.name, dentry->d_name.name, found);
  431. #endif
  432.             goto repeat;
  433.         }
  434.     }
  435.     /*
  436.      * All done at this level ... ascend and resume the search.
  437.      */
  438.     if (this_parent != parent) {
  439.         next = this_parent->d_child.next; 
  440.         this_parent = this_parent->d_parent;
  441. #ifdef DCACHE_DEBUG
  442. printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
  443. this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
  444. #endif
  445.         goto resume;
  446.     }
  447.     return found;
  448. }
  449.  
  450. /*
  451.  * Prune the dcache to remove unused children of the parent dentry.
  452.  */
  453. void shrink_dcache_parent(struct dentry * parent)
  454. {
  455.     int found;
  456.  
  457.     while ((found = select_parent(parent)) != 0)
  458.         prune_dcache(found);
  459. }
  460.  
  461. /*
  462.  * This is called from kswapd when we think we need some
  463.  * more memory, but aren't really sure how much. So we
  464.  * carefully try to free a _bit_ of our dcache, but not
  465.  * too much.
  466.  *
  467.  * Priority:
  468.  *   0 - very urgent: shrink everything
  469.  *  ...
  470.  *   6 - base-level: try to shrink a bit.
  471.  */
  472. void shrink_dcache_memory(int priority, unsigned int gfp_mask)
  473. {
  474.     if (gfp_mask & __GFP_IO)
  475.         prune_dcache(0);
  476. }
  477.  
  478. #define NAME_ALLOC_LEN(len)    ((len+16) & ~15)
  479.  
  480. struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
  481. {
  482.     char * str;
  483.     struct dentry *dentry;
  484.  
  485.     dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
  486.     if (!dentry)
  487.         return NULL;
  488.  
  489.     if (name->len > DNAME_INLINE_LEN-1) {
  490.         str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
  491.         if (!str) {
  492.             kmem_cache_free(dentry_cache, dentry); 
  493.             return NULL;
  494.         }
  495.     } else
  496.         str = dentry->d_iname; 
  497.  
  498.     memcpy(str, name->name, name->len);
  499.     str[name->len] = 0;
  500.  
  501.     dentry->d_count = 1;
  502.     dentry->d_flags = 0;
  503.     dentry->d_inode = NULL;
  504.     dentry->d_parent = NULL;
  505.     dentry->d_sb = NULL;
  506.     if (parent) {
  507.         dentry->d_parent = dget(parent);
  508.         dentry->d_sb = parent->d_sb;
  509.         list_add(&dentry->d_child, &parent->d_subdirs);
  510.     } else
  511.         INIT_LIST_HEAD(&dentry->d_child);
  512.         
  513.     dentry->d_mounts = dentry;
  514.     dentry->d_covers = dentry;
  515.     INIT_LIST_HEAD(&dentry->d_hash);
  516.     INIT_LIST_HEAD(&dentry->d_lru);
  517.     INIT_LIST_HEAD(&dentry->d_subdirs);
  518.     INIT_LIST_HEAD(&dentry->d_alias);
  519.  
  520.     dentry->d_name.name = str;
  521.     dentry->d_name.len = name->len;
  522.     dentry->d_name.hash = name->hash;
  523.     dentry->d_op = NULL;
  524.     dentry->d_fsdata = NULL;
  525.     return dentry;
  526. }
  527.  
  528. /*
  529.  * Fill in inode information in the entry.
  530.  *
  531.  * This turns negative dentries into productive full members
  532.  * of society.
  533.  *
  534.  * NOTE! This assumes that the inode count has been incremented
  535.  * (or otherwise set) by the caller to indicate that it is now
  536.  * in use by the dcache..
  537.  */
  538. void d_instantiate(struct dentry *entry, struct inode * inode)
  539. {
  540.     if (inode)
  541.         list_add(&entry->d_alias, &inode->i_dentry);
  542.     entry->d_inode = inode;
  543. }
  544.  
  545. struct dentry * d_alloc_root(struct inode * root_inode, struct dentry *old_root)
  546. {
  547.     struct dentry *res = NULL;
  548.  
  549.     if (root_inode) {
  550.         res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
  551.         if (res) {
  552.             res->d_sb = root_inode->i_sb;
  553.             res->d_parent = res;
  554.             d_instantiate(res, root_inode);
  555.         }
  556.     }
  557.     return res;
  558. }
  559.  
  560. static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
  561. {
  562.     hash += (unsigned long) parent;
  563.     hash = hash ^ (hash >> D_HASHBITS) ^ (hash >> D_HASHBITS*2);
  564.     return dentry_hashtable + (hash & D_HASHMASK);
  565. }
  566.  
  567. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  568. {
  569.     unsigned int len = name->len;
  570.     unsigned int hash = name->hash;
  571.     const unsigned char *str = name->name;
  572.     struct list_head *head = d_hash(parent,hash);
  573.     struct list_head *tmp = head->next;
  574.  
  575.     for (;;) {
  576.         struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
  577.         if (tmp == head)
  578.             break;
  579.         tmp = tmp->next;
  580.         if (dentry->d_name.hash != hash)
  581.             continue;
  582.         if (dentry->d_parent != parent)
  583.             continue;
  584.         if (parent->d_op && parent->d_op->d_compare) {
  585.             if (parent->d_op->d_compare(parent, &dentry->d_name, name))
  586.                 continue;
  587.         } else {
  588.             if (dentry->d_name.len != len)
  589.                 continue;
  590.             if (memcmp(dentry->d_name.name, str, len))
  591.                 continue;
  592.         }
  593.         return dget(dentry);
  594.     }
  595.     return NULL;
  596. }
  597.  
  598. /*
  599.  * An insecure source has sent us a dentry, here we verify it.
  600.  *
  601.  * This is just to make knfsd able to have the dentry pointer
  602.  * in the NFS file handle.
  603.  *
  604.  * NOTE! Do _not_ dereference the pointers before we have
  605.  * validated them. We can test the pointer values, but we
  606.  * must not actually use them until we have found a valid
  607.  * copy of the pointer in kernel space..
  608.  */
  609. int d_validate(struct dentry *dentry, struct dentry *dparent,
  610.            unsigned int hash, unsigned int len)
  611. {
  612.     struct list_head *base, *lhp;
  613.     int valid = 1;
  614.  
  615.     if (dentry != dparent) {
  616.         base = d_hash(dparent, hash);
  617.         lhp = base;
  618.         while ((lhp = lhp->next) != base) {
  619.             if (dentry == list_entry(lhp, struct dentry, d_hash))
  620.                 goto out;
  621.         }
  622.     } else {
  623.         /*
  624.          * Special case: local mount points don't live in
  625.          * the hashes, so we search the super blocks.
  626.          */
  627.         struct super_block *sb = sb_entry(super_blocks.next);
  628.  
  629.         for (; sb != sb_entry(&super_blocks); 
  630.              sb = sb_entry(sb->s_list.next)) {
  631.             if (!sb->s_dev)
  632.                 continue;
  633.             if (sb->s_root == dentry)
  634.                 goto out;
  635.         }
  636.     }
  637.     valid = 0;
  638. out:
  639.     return valid;
  640. }
  641.  
  642. /*
  643.  * When a file is deleted, we have two options:
  644.  * - turn this dentry into a negative dentry
  645.  * - unhash this dentry and free it.
  646.  *
  647.  * Usually, we want to just turn this into
  648.  * a negative dentry, but if anybody else is
  649.  * currently using the dentry or the inode
  650.  * we can't do that and we fall back on removing
  651.  * it from the hash queues and waiting for
  652.  * it to be deleted later when it has no users
  653.  */
  654. void d_delete(struct dentry * dentry)
  655. {
  656.     /*
  657.      * Are we the only user?
  658.      */
  659.     if (dentry->d_count == 1) {
  660.         dentry_iput(dentry);
  661.         return;
  662.     }
  663.  
  664.     /*
  665.      * If not, just drop the dentry and let dput
  666.      * pick up the tab..
  667.      */
  668.     d_drop(dentry);
  669. }
  670.  
  671. void d_rehash(struct dentry * entry)
  672. {
  673.     struct dentry * parent = entry->d_parent;
  674.  
  675.     list_add(&entry->d_hash, d_hash(parent, entry->d_name.hash));
  676. }
  677.  
  678. #define do_switch(x,y) do { \
  679.     __typeof__ (x) __tmp = x; \
  680.     x = y; y = __tmp; } while (0)
  681.  
  682. /*
  683.  * When switching names, the actual string doesn't strictly have to
  684.  * be preserved in the target - because we're dropping the target
  685.  * anyway. As such, we can just do a simple memcpy() to copy over
  686.  * the new name before we switch.
  687.  *
  688.  * Note that we have to be a lot more careful about getting the hash
  689.  * switched - we have to switch the hash value properly even if it
  690.  * then no longer matches the actual (corrupted) string of the target.
  691.  * The has value has to match the hash queue that the dentry is on..
  692.  */
  693. static inline void switch_names(struct dentry * dentry, struct dentry * target)
  694. {
  695.     const unsigned char *old_name, *new_name;
  696.  
  697.     memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN); 
  698.     old_name = target->d_name.name;
  699.     new_name = dentry->d_name.name;
  700.     if (old_name == target->d_iname)
  701.         old_name = dentry->d_iname;
  702.     if (new_name == dentry->d_iname)
  703.         new_name = target->d_iname;
  704.     target->d_name.name = new_name;
  705.     dentry->d_name.name = old_name;
  706. }
  707.  
  708. /*
  709.  * We cannibalize "target" when moving dentry on top of it,
  710.  * because it's going to be thrown away anyway. We could be more
  711.  * polite about it, though.
  712.  *
  713.  * This forceful removal will result in ugly /proc output if
  714.  * somebody holds a file open that got deleted due to a rename.
  715.  * We could be nicer about the deleted file, and let it show
  716.  * up under the name it got deleted rather than the name that
  717.  * deleted it.
  718.  *
  719.  * Careful with the hash switch. The hash switch depends on
  720.  * the fact that any list-entry can be a head of the list.
  721.  * Think about it.
  722.  */
  723. void d_move(struct dentry * dentry, struct dentry * target)
  724. {
  725.     if (!dentry->d_inode)
  726.         printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  727.  
  728.     /* Move the dentry to the target hash queue */
  729.     list_del(&dentry->d_hash);
  730.     list_add(&dentry->d_hash, &target->d_hash);
  731.  
  732.     /* Unhash the target: dput() will then get rid of it */
  733.     list_del(&target->d_hash);
  734.     INIT_LIST_HEAD(&target->d_hash);
  735.  
  736.     list_del(&dentry->d_child);
  737.     list_del(&target->d_child);
  738.  
  739.     /* Switch the parents and the names.. */
  740.     switch_names(dentry, target);
  741.     do_switch(dentry->d_parent, target->d_parent);
  742.     do_switch(dentry->d_name.len, target->d_name.len);
  743.     do_switch(dentry->d_name.hash, target->d_name.hash);
  744.  
  745.     /* And add them back to the (new) parent lists */
  746.     list_add(&target->d_child, &target->d_parent->d_subdirs);
  747.     list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
  748. }
  749.  
  750. /*
  751.  * "buflen" should be PAGE_SIZE or more.
  752.  */
  753. char * d_path(struct dentry *dentry, char *buffer, int buflen)
  754. {
  755.     char * end = buffer+buflen;
  756.     char * retval;
  757.     struct dentry * root = current->fs->root;
  758.  
  759.     *--end = '\0';
  760.     buflen--;
  761.     if (dentry->d_parent != dentry && list_empty(&dentry->d_hash)) {
  762.         buflen -= 10;
  763.         end -= 10;
  764.         memcpy(end, " (deleted)", 10);
  765.     }
  766.  
  767.     /* Get '/' right */
  768.     retval = end-1;
  769.     *retval = '/';
  770.  
  771.     for (;;) {
  772.         struct dentry * parent;
  773.         int namelen;
  774.  
  775.         if (dentry == root)
  776.             break;
  777.         dentry = dentry->d_covers;
  778.         parent = dentry->d_parent;
  779.         if (dentry == parent)
  780.             break;
  781.         namelen = dentry->d_name.len;
  782.         buflen -= namelen + 1;
  783.         if (buflen < 0)
  784.             break;
  785.         end -= namelen;
  786.         memcpy(end, dentry->d_name.name, namelen);
  787.         *--end = '/';
  788.         retval = end;
  789.         dentry = parent;
  790.     }
  791.     return retval;
  792. }
  793.  
  794. /*
  795.  * NOTE! The user-level library version returns a
  796.  * character pointer. The kernel system call just
  797.  * returns the length of the buffer filled (which
  798.  * includes the ending '\0' character), or a negative
  799.  * error value. So libc would do something like
  800.  *
  801.  *    char *getcwd(char * buf, size_t size)
  802.  *    {
  803.  *        int retval;
  804.  *
  805.  *        retval = sys_getcwd(buf, size);
  806.  *        if (retval >= 0)
  807.  *            return buf;
  808.  *        errno = -retval;
  809.  *        return NULL;
  810.  *    }
  811.  */
  812. asmlinkage int sys_getcwd(char *buf, unsigned long size)
  813. {
  814.     int error;
  815.     struct dentry *pwd = current->fs->pwd; 
  816.  
  817.     error = -ENOENT;
  818.     /* Has the current directory has been unlinked? */
  819.     if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
  820.         char *page = (char *) __get_free_page(GFP_USER);
  821.         error = -ENOMEM;
  822.         if (page) {
  823.             unsigned long len;
  824.             char * cwd = d_path(pwd, page, PAGE_SIZE);
  825.  
  826.             error = -ERANGE;
  827.             len = PAGE_SIZE + page - cwd;
  828.             if (len <= size) {
  829.                 error = len;
  830.                 if (copy_to_user(buf, cwd, len))
  831.                     error = -EFAULT;
  832.             }
  833.             free_page((unsigned long) page);
  834.         }
  835.     }
  836.     return error;
  837. }
  838.  
  839. /*
  840.  * Test whether new_dentry is a subdirectory of old_dentry.
  841.  *
  842.  * Trivially implemented using the dcache structure
  843.  */
  844. int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
  845. {
  846.     int result;
  847.  
  848.     result = 0;
  849.     for (;;) {
  850.         if (new_dentry != old_dentry) {
  851.             struct dentry * parent = new_dentry->d_parent;
  852.             if (parent == new_dentry)
  853.                 break;
  854.             new_dentry = parent;
  855.             continue;
  856.         }
  857.         result = 1;
  858.         break;
  859.     }
  860.     return result;
  861. }
  862.  
  863. /*
  864.  * Check whether a dentry already exists for the given name,
  865.  * and return the inode number if it has an inode.
  866.  *
  867.  * This routine is used to post-process directory listings for
  868.  * filesystems using synthetic inode numbers, and is necessary
  869.  * to keep getcwd() working.
  870.  */
  871. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  872. {
  873.     struct dentry * dentry;
  874.     ino_t ino = 0;
  875.  
  876.     /*
  877.      * Check for a fs-specific hash function. Note that we must
  878.      * calculate the standard hash first, as the d_op->d_hash()
  879.      * routine may choose to leave the hash value unchanged.
  880.      */
  881.     name->hash = full_name_hash(name->name, name->len);
  882.     if (dir->d_op && dir->d_op->d_hash)
  883.     {
  884.         if (dir->d_op->d_hash(dir, name) != 0)
  885.             goto out;
  886.     }
  887.  
  888.     dentry = d_lookup(dir, name);
  889.     if (dentry)
  890.     {
  891.         if (dentry->d_inode)
  892.             ino = dentry->d_inode->i_ino;
  893.         dput(dentry);
  894.     }
  895. out:
  896.     return ino;
  897. }
  898.  
  899. void __init dcache_init(void)
  900. {
  901.     int i;
  902.     struct list_head *d = dentry_hashtable;
  903.  
  904.     /* 
  905.      * A constructor could be added for stable state like the lists,
  906.      * but it is probably not worth it because of the cache nature
  907.      * of the dcache. 
  908.      * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
  909.      * flag could be removed here, to hint to the allocator that
  910.      * it should not try to get multiple page regions.  
  911.      */
  912.     dentry_cache = kmem_cache_create("dentry_cache",
  913.                      sizeof(struct dentry),
  914.                      0,
  915.                      SLAB_HWCACHE_ALIGN,
  916.                      NULL, NULL);
  917.     if (!dentry_cache)
  918.         panic("Cannot create dentry cache");
  919.  
  920.     i = D_HASHSIZE;
  921.     do {
  922.         INIT_LIST_HEAD(d);
  923.         d++;
  924.         i--;
  925.     } while (i);
  926. }
  927.